/**
 * 初始化数据源定义详情对话框
 */
var SdsMedicalGroupInfoDlg = {
    id: "SdsMedicalGroupTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    zTree: null,
    layerIndex: -1,
    linkFlag: false,
    clicked: false,
    SdsMedicalGroupData:  {}
};

SdsMedicalGroupInfoDlg.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭
}

/**
 * 验证数据是否为空
 */
SdsMedicalGroupInfoDlg.validate = function () {
    $('#sdsMedicalGroupInfoForm').data("bootstrapValidator").resetForm();
    $('#sdsMedicalGroupInfoForm').bootstrapValidator('validate');
    return $("#sdsMedicalGroupInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交新增
 */
SdsMedicalGroupInfoDlg.addSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    if (!SdsMedicalGroupInfoDlg.clicked) {
        SdsMedicalGroupInfoDlg.clicked = true;
        var RuleStr = $('#reason').combobox('getValue');
        var Resume = $('#resume').val()
        if ((RuleStr == "") && (Resume == "")) {
            SdsMedicalGroupInfoDlg.clicked = false;
            $.messager.alert("错误提示", "入组原因和备注最少填写一项！", 'info');
            return;
        }
        var params = {};
        var ajax = new $ax("/sds/sdsmedicalgroup/add", function (data) {
            if (parseInt(data) < 1) {
                SdsMedicalGroupInfoDlg.clicked = false;
                $.messager.alert("错误提示", "入单病种失败!", 'info');
                return; //如果主记录保存失败  退出
            } else {
                $.messager.popover({msg: '操作成功！', type: 'success', timeout: 1000});
                window.parent.SdsPatientList.table.refresh();
                SdsMedicalGroupInfoDlg.close();
            }
            SdsMedicalGroupInfoDlg.clicked = false;
        }, function (data) {
            SdsMedicalGroupInfoDlg.clicked = false;
        });
        params.episodeId = $('#episodeId').val();
        params.entityId = $('#entityId').combobox('getValue');
        params.reason = RuleStr;
        params.resume = Resume;
        ajax.set(params);
        ajax.start();
    }
}

SdsMedicalGroupInfoDlg.entityChange = function(source) {
    var sourceId = source.value;
    $('#reason').combobox("clear");
    //根据父级机构 查询子机构
    var ajax = new $ax(Sys.backendPath + "/sds/sdsmedicalgroup/getReasonByEntityId", function(res){
        var reasonMap = $.parseJSON(res);
        $('#reason').combobox({
            data: reasonMap,
            valueField:'value',
            textField:'text',
            onLoadSuccess: function () {
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"entityId":sourceId});
    ajax.setAsync(false);
    ajax.start();
}

$(function() {
    Sys.initValidator("sdsMedicalGroupInfoForm", SdsMedicalGroupInfoDlg.validateFields);
});
